package com.greateffect.littlebud.lib.utils;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.greateffect.littlebud.lib.utilcode.Utils;

import java.io.File;

/**
 * Created by ZengCS on 2017/7/17.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */

public class JUriUtil {
    // public static final String TAG_FILE_PROVIDER = "cn.sxw.android.launcher.fileProvider";
    public static final String TAG_FILE_PROVIDER = Utils.getApp().getPackageName().concat(".FileProvider");

    /**
     * 根据文件路径获取Uri
     */
    public static Uri getUriForFile(Context context, String filePath) {
        return getUriForFile(context, new File(filePath));
    }

    /**
     * 根据文件获取Uri
     */
    public static Uri getUriForFile(Context context, File file) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            return Uri.fromFile(file);
        } else {
            return FileProvider.getUriForFile(context, TAG_FILE_PROVIDER, file);
        }
    }

    /**
     * 单独处理获取图片Uri
     */
    public static Uri getPictureUri(Context context, String filePath) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            return Uri.fromFile(new File(filePath));
        } else {
            ContentValues contentValues = new ContentValues(1);
            contentValues.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        }
    }
}
